<template>
  <div class="border-line card">
    <header-title title="" @fullScreen="handleFullScreen"></header-title>
    <div class="subtext">租赁情况走向</div>
    <div class="subtext" style="margin-left: 1%; width: 49%">{{ `房屋出租情况（全市平均出租率：${pjczl}%）` }}</div>
    <div class="content">
      <div class="column">
        <echarts-view ref="echarts1" :options="option1" height="300px"></echarts-view>
      </div>
      <div class="column">
        <div @mouseenter="clearAutoPlay" @mouseleave="setAutoPlay">
          <echarts-view ref="echarts2" :options="option2" height="300px"></echarts-view>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import headerTitle from './headerTitle.vue'
import { echartsView, Ellipsis } from '@/components'
import axios from 'axios'
import * as echarts from 'echarts'
export default {
  name: 'Right3',
  props: {
    areaID: {
      type: String,
      default: '',
    },
  },
  components: {
    headerTitle,
    echartsView,
    Ellipsis,
  },
  data() {
    return {
      pjczl: 0,
      option1: {},
      option2: {},
      autoPlayer: null,
    }
  },
  mounted() {
    this.fetchData()
  },
  watch: {
    areaID: function () {
      this.fetchData()
    },
  },
  methods: {
    fetchData() {
      const echart1 = axios({
        method: 'get',
        timeout: 180000,
        url: process.env.VUE_APP_API_BASE_URL_BZF + '/api/Statistics/TJRY_ZLQKZX',
        params: {
          Userid: 1,
          QY: this.areaID,
        },
      })
      const echart2 = axios({
        method: 'get',
        timeout: 180000,
        url: process.env.VUE_APP_API_BASE_URL_BZF + '/api/Statistics/TJFY_FWCLQK',
        params: {
          Userid: 1,
          QY: this.areaID,
          TJ: 'TJ',
        },
      })
      Promise.all([echart1, echart2]).then((res) => {
        const xAxisData1 = []
        const yrData = []
        res[0].data.forEach((item) => {
          xAxisData1.push(item.name)
          yrData.push(Number(item.value))
        })
        const max = Math.max(...yrData)
        const xAxisData2 = []
        const fwzs = []
        const kzzs = []
        const czzs = []
        const czl = []
        res[1].data.TJinfo.forEach((item) => {
          xAxisData2.push(item.name)
          fwzs.push(Number(item.FWCOUNT))
          kzzs.push(Number(item.FWZT))
          czzs.push(Number(item.namemc))
          czl.push(Number(item.HX))
        })
        this.pjczl = res[1].data.num
        this.option1 = {
          visualMap: {
            show: false,
            type: 'continuous',
            seriesIndex: 0,
            min: 0,
            max: max,
          },
          tooltip: {
            trigger: 'axis',
            confine: true,
          },
          grid: {
            left: 0,
            right: 15,
            top: 40,
            bottom: '3%',
            containLabel: true,
          },
          xAxis: {
            type: 'category',
            boundaryGap: false,
            data: xAxisData1,
          },
          yAxis: {
            type: 'value',
            splitLine: {
              show: false,
            },
          },
          dataZoom: [
            // 滑动条
            {
              show: false, // 是否显示滑动条
              type: 'slider', // 这个 dataZoom 组件是 slider 型 dataZoom 组件
              startValue: 0, // 从头开始。
              endValue: 11, // 一次性展示几个。
            },
          ],
          series: [
            {
              data: yrData,
              name: '月入住量',
              type: 'line',
            },
          ],
        }
        this.option2 = {
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'shadow',
            },
          },
          grid: {
            left: 30,
            right: 0,
            top: 40,
            bottom: '3%',
            containLabel: true,
          },
          dataZoom: [
            // 滑动条
            {
              show: false, // 是否显示滑动条
              type: 'slider', // 这个 dataZoom 组件是 slider 型 dataZoom 组件
              startValue: 0, // 从头开始。
              endValue: 5, // 一次性展示几个。
            },
          ],
          xAxis: [
            {
              type: 'category',
              data: xAxisData2,
              axisTick: {
                // 刻度线
                show: false,
              },
              axisLine: {
                // 坐标轴
                show: false,
              },
              splitLine: {
                // 网格线
                show: false,
              },
            },
          ],
          yAxis: [
            {
              type: 'value',
              splitLine: {
                show: false,
              },
            },
            {
              type: 'value',
              show: false,
            },
          ],
          series: [
            {
              name: '建成套数',
              type: 'bar',
              barGap: 0,
              emphasis: {
                focus: 'series',
              },
              data: fwzs,
            },
            {
              name: '空置套数',
              type: 'bar',
              barGap: 0,
              emphasis: {
                focus: 'series',
              },
              data: kzzs,
            },
            {
              name: '出租套数',
              type: 'bar',
              barGap: 0,
              emphasis: {
                focus: 'series',
              },
              data: czzs,
            },
            {
              name: '出租率',
              type: 'line',
              yAxisIndex: 1,
              smooth: true, // 是否平滑
              showAllSymbol: true,
              symbol: 'circle',
              symbolSize: 16,
              lineStyle: {
                normal: {
                  color: '#f36c6c',
                  width: 5,
                },
              },
              zlevel: 1,
              label: {
                show: false,
                position: 'top',
                textStyle: {
                  color: '#6c50f3',
                },
              },
              itemStyle: {
                // color: '#fff',
                // borderColor: '#F6AE31',
                color: '#f36c6c',
                borderColor: '#fff',
                borderWidth: 3,
              },
              areaStyle: {
                normal: {
                  color: new echarts.graphic.LinearGradient(
                    0,
                    0,
                    0,
                    1,
                    [
                      {
                        offset: 0,
                        color: 'rgba(243, 108, 108,0.8)',
                      },
                      {
                        offset: 1,
                        color: 'rgba(239, 170, 52, 0.02)',
                      },
                    ],
                    false
                  ),
                  shadowColor: 'rgba(0, 96, 255, 0.9)',
                  shadowBlur: 10,
                },
              },
              tooltip: {
                valueFormatter: function (value) {
                  return value + '%'
                },
              },
              data: czl,
            },
          ],
        }
        this.$nextTick(() => {
          this.setAutoPlay()
        })
      })
    },
    setAutoPlay() {
      this.clearAutoPlay('setAutoPlay')
      this.autoPlayer = setInterval(() => {
        if (this.option2.dataZoom[0].endValue === this.option2.series[0].data.length) {
          this.option2.dataZoom[0].endValue = 5
          this.option2.dataZoom[0].startValue = 0
        } else {
          this.option2.dataZoom[0].endValue = this.option2.dataZoom[0].endValue + 1
          this.option2.dataZoom[0].startValue = this.option2.dataZoom[0].startValue + 1
        }
      }, 3000)
    },
    clearAutoPlay() {
      if (!this.$isEmpty(this.autoPlayer)) {
        clearInterval(this.autoPlayer)
      }
    },
    handleFullScreen() {
      this.$emit('fullScreen')
    },
  },
}
</script>
<style lang="less" scoped>
.card {
  height: 400px;
  font-size: 18px;
  .content {
    display: flex;
    flex-direction: row;
    .column {
      flex: 1;
      .table {
        .col {
          flex: 1;
          font-size: 20px;
        }

        .header {
          background: #0f3560;
          font-size: 16px;
          padding: 0 18px;
          color: #fff;
          display: flex;
          justify-content: space-between;
          height: 40px;
          line-height: 40px;
        }

        .scroll-warp {
          height: 248px;
          width: 100%;
          margin: 0 auto;
          overflow: hidden;
          ul {
            list-style: none;
            padding: 0;
            margin: 0 auto;
            li {
              height: 40px;
              line-height: 40px;
              padding: 0 18px;
              color: #61a8ff;
              position: relative;
              font-size: 14px;
              display: flex;
              justify-content: space-between;
              &:nth-child(2n) {
                background: rgb(8, 38, 91);
              }
            }
          }
        }
      }
    }
  }
}
.subtext {
  color: #fff;
  background-color: #0d5b78;
  display: inline-block;
  width: 50%;
  font-size: 20px;
  box-sizing: border-box;
  padding: 5px 10px;
  margin: 6px 0;
}
</style>
